const initialState = {
    displayNum: '0',
    lastDisplayNum: null,
    cal: {
      "+": false,
      "-": false,
      "x": false,
      "÷": false,
      "=": false
    }
}

// reducer
export default (state = initialState, action) => {
    switch (action.type) {
        case "INPUT_NUMBER":
            let displayNum = '';
            let lastDisplayNum = state.lastDisplayNum
            if (state.displayNum !== '0') {
                displayNum = state.displayNum;
            }
            for (let key in state.cal){
                if (state.cal[key] && !state.lastDisplayNum){
                if (key !== '='){
                    lastDisplayNum = state.displayNum;
                }
                displayNum = ''
                break;
                }
            }
            if (displayNum.length >= 10){
                return {
                    ...state
                };
            }
            return {
                ...state,
                lastDisplayNum,
                displayNum: displayNum + action.payload
            };
        default:
            break;
    }
    return state;
};